package demo29;

import java.util.LinkedList;
import java.util.List;

/*将判断和操作外面加锁，程序完全没有功能上的问题，但是效率很低*/
public class TicketSeller3 {
    static List<String> tickets = new LinkedList<>();

    static {
        for (int i = 0; i < 100; i++) {  //共100张票
            tickets.add("票编号:" + i);
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {   //共10个线程卖票
            new Thread(() -> {
                while (true) {
                    synchronized (tickets) {
                        if (tickets.size() <= 0) break;  //判断 余票
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("销售了--" + tickets.remove(0)); //操作减票
                    }
                }

            }).start();
        }
    }
}